package service

import (
	"context"
	"fmt"
	"github.com/micro/go-micro/v2/errors"
	pb "micro-grpc-server/services/music/proto"
)

// 搜索歌曲
func (s *MusicService) MusicSearch(ctx context.Context, rq *pb.ListMusicRequest, resp *pb.ListMusicResponse) (err error) {
	srv := s.dao.GetScenarioServer(rq.Scenario)
	if srv == nil {
		return errors.BadRequest(s.conf.ServerName, fmt.Sprintf("参数错误！场景 `%s` 不存在", rq.Scenario))
	}
	resp.Scenario = rq.Scenario
	if resp.List, resp.Total, err = srv.Search(rq); err != nil {
		return errors.BadRequest(s.conf.ServerName, fmt.Sprintf("服务不可用！%s", err.Error()))
	}
	return nil
}

// 获取播放信息
func (s *MusicService) GetPlayInfo(ctx context.Context, rq *pb.PLayInfoRequest, resp *pb.PlayInfoResponse) (err error) {
	srv := s.dao.GetScenarioServer(rq.Scenario)
	if srv == nil {
		return errors.BadRequest(s.conf.ServerName, fmt.Sprintf("参数错误！场景 `%s` 不存在", rq.Scenario))
	}
	if resp.Url, resp.Lyric, err = srv.GetPlayInfo(rq); err != nil {
		return errors.BadRequest(s.conf.ServerName, fmt.Sprintf("服务不可用！%s", err.Error()))
	}
	return nil
}

// 获取歌词
func (s *MusicService) GetLyric(ctx context.Context, rq *pb.LyricRequest, resp *pb.LyricResponse) (err error) {
	srv := s.dao.GetScenarioServer(rq.Scenario)
	if srv == nil {
		return errors.BadRequest(s.conf.ServerName, fmt.Sprintf("参数错误！场景 `%s` 不存在", rq.Scenario))
	}
	resp.Lyric, resp.Hit = srv.GetLyric(rq)
	return nil
}
